package com.kkd.kkdapp.activity;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.EvetyDayJoinRecordAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.EvetyDayJoinRecordData;
import com.kkd.kkdapp.http.entity.EvetyDayJoinRecordDatalist;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;

import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 天天赚加入记录
 * 
 * @author mty
 * 
 */
public class EvetyDayJoinRecordActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private RelativeLayout mReturn; // 返回
	private KkdXlistView mXListView;
	private int pageNO = 1;
	private int pageSize = 10;
	// private String version;
	private String yuebaoid; // 天天赚ID
	private List<EvetyDayJoinRecordDatalist> dataList;
	private EvetyDayJoinRecordAdapter mAdapter;
	private ImageView null_img;
	private TextView null_tv;
	private boolean REFRESHFLAG = true;
	private Time_Date time_Date = new Time_Date();
	private String desc;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			String time = "";
			switch (msg.what) {
			case 1:
				mXListView.stopLoadMore(-1);
				if (REFRESHFLAG) {
					mXListView.stopLoadMore(1);
				}
				break;
			case 2:
				mAdapter.update();
				mAdapter.appendData(dataList, pageNO == 1);
				mAdapter.notifyDataSetChanged();
				mXListView.setNoMore(false);
				if (dataList.size() < pageSize) {
					mXListView.stopLoadMore(1);
				} else {
					mXListView.stopLoadMore(0);
				}
				time = time_Date.get_Year_Day();
				mXListView.setRefreshTime(time);
				if (dataList == null || dataList.size() == 0) {
					null_img.setVisibility(View.VISIBLE);
					null_tv.setVisibility(View.VISIBLE);
				} else {
					null_img.setVisibility(View.GONE);
					null_tv.setVisibility(View.GONE);
				}
				break;
			case 3:
				mXListView.stopRefresh();
				time = time_Date.get_Year_Day();
				mXListView.setRefreshTime(time);
				break;
			case 4:
				showToast("加载失败");
				break;
			case 5:
				if (REFRESHFLAG) {
					if (dataList == null) {
						null_img.setVisibility(View.VISIBLE);
						null_tv.setVisibility(View.VISIBLE);
						// mXListView.setPullLoadEnable(false);
					} else {
						// mXListView.setPullLoadEnable(true);
						null_img.setVisibility(View.GONE);
						null_tv.setVisibility(View.GONE);
					}
				}
				break;
			case 6:
				mXListView.stopRefresh();
				mXListView.stopLoadMore(1);
				break;
			default:
				break;
			}
		};
	};
	private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evety_day_join_record);
		initView();
		setListener();
	}

	/*
	 * 初始化组件
	 */
	public void initView() {
		mReturn = (RelativeLayout) this.findViewById(R.id.title_img);
		mXListView = (KkdXlistView) this.findViewById(R.id.condition_lv);
		mAdapter = new EvetyDayJoinRecordAdapter(this);
		null_img = (ImageView) this.findViewById(R.id.null_img);
		null_tv = (TextView) this.findViewById(R.id.null_tv);

		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		mListView.setAdapter(mAdapter);
		mXListView.startRefresh();
		getMessager();
		queryYuebaoZRTradeWater();
	}

	/**
	 * 注册监听
	 */
	public void setListener() {
		mReturn.setOnClickListener(this);
		// mXListView.setXListViewListener(this);
		// mXListView.setPullRefreshEnable(true);
		if (AppUtil.isConnect(this)) {
			null_img.setVisibility(View.GONE);
			null_tv.setVisibility(View.GONE);
			// mXListView.setPullLoadEnable(true);
		} else {
			null_img.setVisibility(View.VISIBLE);
			null_tv.setVisibility(View.VISIBLE);
			// mXListView.setPullLoadEnable(false);
		}
	}

	/**
	 * 获取上一页传过来的消息
	 */
	public void getMessager() {
		Intent intent = getIntent();
		yuebaoid = intent.getStringExtra("yuebaoid");
		LogUtil.e("YuebaoId", yuebaoid);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_img:
			finish();
			break;
		}
	}

	/**
	 * 天天赚加入记录
	 */
	public void queryYuebaoZRTradeWater() {

		KkdHttpMagager.getInstance().getEvetyDayJoinRecordManager(yuebaoid,
				pageNO, pageSize, 
				EvetyDayJoinRecordData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {

						EvetyDayJoinRecordData mEvetyDayJoinRecordData = (EvetyDayJoinRecordData) responseBody;
						int pageNO = mEvetyDayJoinRecordData.getPageNO();
						int pageCount = mEvetyDayJoinRecordData.getPageCount();
						if (pageNO > pageCount) {
							handler.sendEmptyMessage(1);
						} else {
							dataList = mEvetyDayJoinRecordData.getDataList();
							handler.sendEmptyMessage(2);
						}
						handler.sendEmptyMessage(3);

					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = 4;
						msg.obj = desc;
						handler.sendEmptyMessage(4);
						handler.sendEmptyMessage(5);
						handler.sendEmptyMessage(6);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		queryYuebaoZRTradeWater();
		if (dataList == null || dataList.size() == 0) {
			null_img.setVisibility(View.VISIBLE);
			null_tv.setVisibility(View.VISIBLE);
		} else {
			null_img.setVisibility(View.GONE);
			null_tv.setVisibility(View.GONE);
		}
	}

	@Override
	public void onLoadMore() {
		LogUtil.e("dsddasfasf", "dsddasfasf");
		pageNO++;
		REFRESHFLAG = false;
		queryYuebaoZRTradeWater();
	}

	// @Override
	// public void onRefresh() {
	//
	// pageNO = 1;
	// REFRESHFLAG = true;
	// queryYuebaoZRTradeWater();
	// if (dataList == null) {
	// null_img.setVisibility(View.VISIBLE);
	// null_tv.setVisibility(View.VISIBLE);
	// } else {
	// null_img.setVisibility(View.GONE);
	// null_tv.setVisibility(View.GONE);
	// }
	// }
	//
	// @Override
	// public void onLoadMore() {
	//
	// LogUtil.e("dsddasfasf", "dsddasfasf");
	// pageNO++;
	// REFRESHFLAG = false;
	// queryYuebaoZRTradeWater();
	// }
}
